package com.hblack.manage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hblack.manage.pojo.ContentCategory;
import com.hblack.manage.service.ContentCategoryService;

@Controller
@RequestMapping(value="content/category")
public class ContentCategoryController {

	@Autowired
	private ContentCategoryService contentCategoryService;
	
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<List<ContentCategory>> queryContentCategoryByParentId(@RequestParam(value = "id",defaultValue="0")Long parentId ){
		
		try {
			ContentCategory contentCategory = new ContentCategory();
			contentCategory.setParentId(parentId);
			List<ContentCategory> list = this.contentCategoryService.queryListByWhere(contentCategory);
			
			if(list != null && list.size() != 0){
				return ResponseEntity.ok(list);
			}else {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> saveContentCategoty(ContentCategory contentCategory){
		
		try {
			Boolean saveBoolean = this.contentCategoryService.saveContentCategory(contentCategory);
			if(saveBoolean){
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateContentCategoty(ContentCategory contentCategory){
		
		try {
			contentCategory.setCreated(null);
			contentCategory.setIsParent(null);
			contentCategory.setStatus(null);
			contentCategory.setParentId(null);
			contentCategory.setSortOrder(null);
			Integer count = this.contentCategoryService.updateByPrimaryKey(contentCategory);
			if(count == 1){
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteContentCategoty(ContentCategory contentCategory){
		
		try {
			
			Boolean deleteBoolean = this.contentCategoryService.deleteContentCategory(contentCategory);
			if(deleteBoolean){
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
}
